﻿namespace Okazuki.MVVM.PrismSupport.Interactivity
{
    using System;
    using System.Windows;
    using System.Windows.Interactivity;
    using Microsoft.Practices.Prism.Interactivity.InteractionRequest;

    /// <summary>
    /// メッセージボックスを表示させます。
    /// </summary>
    [DefaultTrigger(typeof(DependencyObject), typeof(InteractionRequestTrigger), null)]
    public class ShowMessageBoxAction : DispatcherTriggerAction
    {
        private static readonly ShowMessageBoxConfirmation NullObject = new ShowMessageBoxConfirmation();

        public static readonly DependencyProperty ButtonProperty =
            DependencyProperty.Register(
                "Button", 
                typeof(MessageBoxButton), 
                typeof(ShowMessageBoxAction),
                new PropertyMetadata(MessageBoxButton.OK));
#if !SILVERLIGHT
        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register(
                "Icon", 
                typeof(MessageBoxImage), 
                typeof(ShowMessageBoxAction),
                new PropertyMetadata(MessageBoxImage.None));

        public static readonly DependencyProperty OptionsProperty =
            DependencyProperty.Register(
                "Options", 
                typeof(MessageBoxOptions), 
                typeof(ShowMessageBoxAction),
                new PropertyMetadata(MessageBoxOptions.None));

#endif
        public static readonly DependencyProperty DefaultResultProperty =
            DependencyProperty.Register(
                "DefaultResult", 
                typeof(MessageBoxResult), 
                typeof(ShowMessageBoxAction),
                new PropertyMetadata(MessageBoxResult.None));

        public static readonly DependencyProperty ContentProperty =
           DependencyProperty.Register(
               "Content",
               typeof(string),
               typeof(ShowMessageBoxAction),
               new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register(
                "Title",
                typeof(string),
                typeof(ShowMessageBoxAction),
                new PropertyMetadata(string.Empty));
        
        /// <summary>
        /// 表示するボタン
        /// </summary>
        public MessageBoxButton Button
        {
            set
            {
                this.SetValue(ButtonProperty, value);
            }
            get
            {
                return (MessageBoxButton)this.GetValue(ButtonProperty);
            }
        }
#if !SILVERLIGHT
        /// <summary>
        /// 表示するアイコン
        /// </summary>
        public MessageBoxImage Icon
        {
            set
            {
                this.SetValue(IconProperty, value);
            }
            get
            {
                return (MessageBoxImage)this.GetValue(IconProperty);
            }
        }

        /// <summary>
        /// オプション
        /// </summary>
        public MessageBoxOptions Options
        {
            set
            {
                this.SetValue(OptionsProperty, value);
            }
            get
            {
                return (MessageBoxOptions)this.GetValue(OptionsProperty);
            }
        }
#endif
        /// <summary>
        /// 既定の戻り値
        /// </summary>
        public MessageBoxResult DefaultResult
        {
            set
            {
                this.SetValue(DefaultResultProperty, value);
            }
            get
            {
                return (MessageBoxResult)this.GetValue(DefaultResultProperty);
            }
        }

        /// <summary>
        /// メッセージボックスのタイトル
        /// </summary>
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        /// <summary>
        /// メッセージボックスに表示するテキスト
        /// </summary>
        public string Content
        {
            get { return (string)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        protected override void InvokeAction(InteractionRequestedEventArgs e)
        {
            var notification = e.Context as ShowMessageBoxConfirmation ?? NullObject;

            var result = MessageBox.Show(
                e.Context.GetMessage() ?? this.Content,
                e.Context.Title ?? this.Title,
                notification.Button ?? this.Button,
                notification.Icon ?? this.Icon,
                notification.DefaultResult ?? this.DefaultResult,
                notification.Options ?? this.Options);

            var conf = e.Context as Confirmation;
            if (conf != null)
            {
                // 同意したときにtrueにする。
                conf.Confirmed = result == MessageBoxResult.OK || result == MessageBoxResult.Yes;
            }

            e.Callback();
        }
    }
}
